// pages/distribution/put-money.js
const app = getApp()
const pool = new Map()
const tabs = [{
    name: "全部",
    status: -10
},
{
    name: "待审核",
    status: 0
},
{
    name: "已打款",
    status: 1
},
{
    name: "待打款",
    status: 2
},
{
    name: "无效",
    status: -1
},
]
Page({

    /**
     * 页面的初始数据
     */
    data: {
        status: -10
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        let statusObj = {}
        Object.keys(tabs).forEach(k => {
            statusObj[tabs[k].status] = tabs[k].name
        })
        this.setData({
            statusObj,
            tabs
        })
        this.loadData()
    },
    clickTab(evt) {
        const status = evt.currentTarget.dataset.status
        if (status != this.data.status) {
            this.setData({
                status
            })
            let tab = pool.get(this.data.status)
            if (tab && tab.list.length == 0) {
                tab.page = 1
                tab.total = 2
            }
            this.loadData()
        }
    },
    loadData() {
        let tab = pool.get(this.data.status)
        if (!tab) {
            tab = { page: 1, nextPage: true, list: [] }
            pool.set(this.data.status, tab)
        }
        this.setData(tab)
        if (!tab.nextPage) {
            this.setData(tab)
            return
        }

        if (tab.page == 1) {
            wx.showLoading({
                title: '加载中...',
            })
        }

        if (this.data.loading) {
            return
        }

        this.setData({
            loading: true
        })

        app.http.post("distributor/cash-list?page=" + tab.page, {
            data: { status: this.data.status, }
        }).then(res => {
            tab.list.push(...res.data.list)
            tab.page++
            tab.nextPage = res.data.nextPage
            this.setData(tab)
            wx.hideLoading()
            wx.stopPullDownRefresh()
            this.setData({
                loading: false
            })
        }).catch(err => {
            app.wxToast.error(err.message)
            this.setData({
                loading: false
            })
        })
    },
    onReachBottom() {
        this.loadData()
    }
})